/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.utils;

import com.flexengine.logger.Logger;
import com.flexengine.xml.Persister;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Luann Athayde
 */
public class FlexGameManager {

    private static final Map<String, FlexGame> games = new HashMap<>();

    public static void loadGames() {
        Logger.logMessage(FlexGameManager.class, "loadResources", "Loading games...");
        File gamesFolder = new File(ResourceManager.getLocale("games/"));
        Logger.logMessage(FlexGameManager.class, "loadResources", "gamesFolder = " + gamesFolder);
        if (gamesFolder.exists() && gamesFolder.canRead()) {
            Persister p = new Persister();
            for (File gameFile : gamesFolder.listFiles(new FlexGameFileFilter())) {
                try {
                    FlexGame game = p.read(FlexGame.class, gameFile);
                    Logger.logMessage(FlexGameManager.class, "loadResources", "Game [" + game + "] loaded!");
                    games.put(game.getName(), game);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        Logger.logMessage(FlexGameManager.class, "loadResources", "Games loaded!");
    }

    public static Map<String, FlexGame> getGames() {
        return games;
    }

    public FlexGame getGame(String name) {
        return games.get(name);
    }

    public FlexGame putGame(String name, FlexGame game) {
        return games.put(name, game);
    }
}
